﻿/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://github.com/shenyczz/CSharpKit
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/
using System;
using System.Security.Cryptography;

namespace CSharpKit.Numerics
{
    /// <summary>
    /// 随机数种子
    /// </summary>
    public static class RandomSeed
    {
        static RandomSeed() { }
        private static readonly object Lock = new object();
        private static readonly RandomNumberGenerator RNG = RandomNumberGenerator.Create();

        /// <summary>
        /// 功能：提供一个时间相关的种子值<br/>
        /// 备注：频繁多次调用会导致相同的种子，不可用于密码学。
        /// </summary>
        public static int Time()
        {
            return Environment.TickCount;
        }

        /// <summary>
        /// 功能：提供一个时间相关的 GUID （全局唯一标识符）<br/>
        /// 备注：频繁多次调用会导致相同的种子，不可用于密码学。
        /// </summary>
        public static int Guid()
        {
            return Environment.TickCount ^ System.Guid.NewGuid().GetHashCode();
        }

        /// <summary>
        /// 功能：提供基于内部随机数生成器（如果可用的话可以提供密码）、时间和唯一GUID的种子。
        /// 备注：该种子只有中等的随机性，快速重复会导致不同的种子值，请勿用于密码学！
        /// </summary>
        public static int Robust()
        {
            lock (Lock)
            {
                var bytes = new byte[4];
                RNG.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            }
        }


        //}}@@@
    }


}
